//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Text;
using Eas.WebCrawler.Interfaces;

namespace Eas.WebCrawler.PlugIns.Selectors {
    /// <summary>A selector which is passed a boolean and always returns results based on that boolean value.</summary>
    /// <remarks>This class is used in conjunction with <see cref="ConditionalSelector"/>.</remarks>
    /// <seealso cref="ConditionalSelector"/>
    public class ConstantSelector : Eas.WebCrawler.Interfaces.LinkSelector{
        /// <summary>A selector which always returns <code>true</code> (always follow links).</summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly ConstantSelector TrueSelector = new ConstantSelector(true);
        /// <summary>A selector which always returns <code>false</code> (never follow links).</summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes")]
        public static readonly ConstantSelector FalseSelector = new ConstantSelector(false);

        /// <summary>The value to always be returned by this selector.</summary>
        /// <remarks>Backing field for the Value property.</remarks>
        /// <seealso cref="Value"/>
        private bool _Value;

        /// <summary>Constructs a <see cref="ConstantSelector"/> which always returns <paramref name="constantValue"/> from calls to <see cref="ShouldFollow"/>.</summary>
        /// <param name="constantValue"></param>
        public ConstantSelector(bool constantValue) {
            _Value = constantValue;
        }

        /// <summary>The value to always be returned by this selector.</summary>
        public bool Value {
            get { return _Value; }
        }
        
        #region LinkSelector Members

        /// <summary>Determines whether or not a <see cref="Request"/> (link) should be crawled.</summary>
        /// <param name="request">The request which has just been encountered.</param>
        /// <returns><code>true</code> if the request should be crawled, otherwise <code>false</code>.</returns>
        /// <remarks>This implementation always returns <see cref="Value"/>.</remarks>
        public bool ShouldFollow(Request request) {
            return _Value;
        }

        #endregion

        #region WebCrawlerComponent Members
        /// <summary>Initializes this class</summary>
        /// <param name="context">The <see cref="WebCrawlerContext"/> which will contain data about this instance of the web crawler.</param>
        public void Initialize(WebCrawlerContext context) { }
        #endregion
    }
}
